/**
 *   Copyright 2008 Alexey Vishentsev
 *
 *   Licensed under the Apache License, Version 2.0 (the &quot;License&quot;);
 *   you may not use this file except in compliance with the License.
 *   You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 *   Unless required by applicable law or agreed to in writing, software
 *   distributed under the License is distributed on an "AS IS" BASIS,
 *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *   See the License for the specific language governing permissions and
 *   limitations under the License.
 * 
 */

package com.gwtspreadsheetinput.jsf.component;

import javax.el.ValueExpression;
import javax.faces.component.UIInput;
import javax.faces.context.FacesContext;

public class SpreadSheetColumn extends UIInput{
	public static final String COMPONENT_TYPE = 
		"com.gwtspreadsheetinput.jsf.SpreadSheetColumn";

	public static final String COMPONENT_FAMILY = 
		"com.gwtspreadsheetinput.jsf.SpreadSheetColumn";
	
	private String title;
	private int width = -1;
	private Object[] values;
	
	public SpreadSheetColumn() {
		super();
		setRendererType("com.gwtspreadsheetinput.jsf.component.SpreadSheetColumnRenderer");
	}
	
	public String getFamily() {
		return COMPONENT_FAMILY;
	}

	public String getTitle() {
		if (this.title != null) {
			return this.title;
		}
		ValueExpression ve = getValueExpression("title");
		if (ve != null) {
			return String.valueOf(ve.getValue(getFacesContext().getELContext()));
		}
		return title;
	}

	public void setTitle(String title) {
		this.title = title;
	}

	public int getWidth() {
		return width;
	}

	public void setWidth(int width) {
		this.width = width;
	}
	
	@Override
	public Object saveState(FacesContext context) {
		if (values == null) {
			values = new Object[3];
		}
		values[0] = super.saveState(context);
		values[1] = title;
		values[2] = new Integer(width);
		
		return values;
	}
	
	@Override
	public void restoreState(FacesContext context, Object state) {
		values = (Object[]) state;
		super.restoreState(context, values[0]);
		title = String.valueOf(values[1]);
		width = ((Integer)values[2]).intValue();
	}
}
